package com.ithao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ithao.dao.IndustryDao;
import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.pojo.Industry;
import com.ithao.service.IndustryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description ：企业行业方向业务实现类
 * @version: 1.0
 */
@Service
@Slf4j
public class IndustryServiceImpl implements IndustryService {
    @Autowired
    private IndustryDao industryDao;

    @Override
    public List<Industry> findListAll() {
        List<Industry> industryList = industryDao.selectListAll();
        return industryList;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[分页查找行业方向] queryPageBean: {}", queryPageBean);
        //开始分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //分页查询数据库
        Page<Industry> page = industryDao.selectPage(queryPageBean.getQueryParams());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        //先删企业与方向的关系
        if (industryDao.countIndusAndCom(id) > 0) {
            industryDao.deleteIndusAndComByIndusId(id);
        }
        //删Industry
        industryDao.delete(id);
    }

    @Override
    public void add(Industry industry) {
        industryDao.add(industry);
    }
}
